package com.j.lemon.learn.basic.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(7777);
        try{
            while (true){
                System.out.println("等待客户端连接");
                Socket socket=serverSocket.accept();
                System.out.println(socket.getInetAddress()+"已经连接！"+">"+socket.getPort());

//                InputStream is = socket.getInputStream();
//                InputStreamReader isr = new InputStreamReader(is);
//                BufferedReader br = new BufferedReader(isr);
//                System.out.println("客户端说："+br.readLine());
                OutputStream o =socket.getOutputStream();
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(o);
                PrintWriter printWriter = new PrintWriter(outputStreamWriter,true);
                String timeS = "2022-01-19 14:41:01";
                while (true){

                    LocalDateTime parse = LocalDateTime.parse(timeS, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                    Date from = Date.from(parse.toInstant(ZoneOffset.of("+8")));

                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(from);
                    calendar.add(Calendar.SECOND,1);
                    Date time = calendar.getTime();

                    LocalDateTime localDateTime = time.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                    String timeStr = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                    timeS = timeStr;

                    String s  = "1,张三,"+timeStr;
                    printWriter.println(s);
                    Thread.sleep(1000);
                }
            }
        }catch (IOException | InterruptedException e){
            e.getMessage();

        } finally {
            serverSocket.close();
        }
    }
}
